<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>

<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ page import="java.util.Date"%>
<% 
   String gridId = "grid_" + new Date().getTime();
   String path = request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);
%>
<script type="text/javascript">
$(function (){
	
	var grid = $('[data-role="esIndexGrid"]');
    
  /*   $("#addDateID").datetimepicker({
		format : "yyyy-mm-dd",
		autoclose : true,
		todayBtn : true,
		minView : 2,
		language : 'zh-CN'
	}); */
	PageLoader = {
	    initSearchPanel:function(){
	    	/* initeICDTypeList("addICD10TypeID","addICD9TypeID",form) ; */
	    },
	    initGridPanel: function(){
	         var self = this;
	         var width = 152;
	        
	         return grid.grid({
	                identity:"salesId",
	                buttons: [
	                        {content: '<button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-plus"><span>添加</button>', action: 'add'},
	                        {content: '<button class="btn btn-success" type="button"><span class="glyphicon glyphicon-edit"><span>修改</button>', action: 'modify'},
	                     /*    {content: '<button class="btn btn-danger" type="button"><span class="glyphicon glyphicon-remove"><span>删除</button>', action: 'delete'} */
	                    ],
	                url:"${pageContext.request.contextPath}/EsIndex/pageJson.dhc",
	                columns: [
               	    { title: '业务编码', 
                 	   width:'11.1%',
                        name: 'salesId'
                      },
                	 { title: '业务名称', 
                	   width:'11.1%',
                       name: 'salesName'
                     },
                	 { title: 'ES索引', 
                    	 width:'11.1%',
                	  name: 'esIndex'
                		 
                	 },
     	             { title: 'ES服务器IP地址', 
                		 width:'11.1%',
                	   name: 'esServerip'
                	 }, { title: '创建ES端口',
                		 width:'11.1%',
                  	   name: 'esCreatePort'
                  	 },
     	             { title: 'ES服务端口',
                		 width:'11.1%',
                	   name: 'esPort'
                	 }, { title: 'ES索引字段',
                		 width:'11.1%',
                       name: 'esFildes', 
                	   render: function(item,n,name){
                		  var fildes=item['esFildes'];
                		  if(fildes!=null&&fildes.length>50){
                			  fildes=fildes.substr(0,50);
                		  }
                   		return fildes;
                  		}
                	 }, { title: '操作',
                		 width:'11.1%',
                		  name:'cao',
                  	   	  render: function(item,n,name){
                   	   		return	 "<a href=\"javascript:createEsIndex('"+item.esIndex+"')\"><span class=\"label label-success\">创建ES索引</span></a> ";
                   	   	 }
                   	 },
                	 { title: '备注', 
                   		 width:'11.1%',
                       name: 'remark'
                     }]
	             }).on({
	                   'add': function(){
	                       self.add($(this));
	                   },
	                   'modify': function(event, data){
	                        var indexs = data.data;
	                        var $this = $(this);
	                        if(indexs.length == 0){
	                            drgUI.warning('请选择一条记录进行修改');
	                            return;
	                        }
	                        if(indexs.length > 1){
	                             drgUI.warning('只能选择一条记录进行修改');
	                            return;
	                        }
	                       self.modify(data.item[0],indexs[0], $this);
	                    },
	                   'delete': function(event, data){
	                        var indexs = data.data;
	                        var $this = $(this);
	                        if(indexs.length == 0){
	                            drgUI.warning('请选择要删除的记录！');
	                            return;
	                        }
	                        var remove = function(){
	                            self.remove(indexs, $this);
	                        };
	                        drgUI.confirm({
	                            content: '确定要删除所选记录吗?',
	                            callSure: remove
	                        });
	                   }
	         });
	         
	    },
	    add: function(grid){
	        var self = this;
	        var dialog = $('<div class="modal fade"><div class="modal-dialog">'
	        	+'<div class="modal-content"><div class="modal-header"><button type="button" class="close" '
	        	+'data-dismiss="modal" aria-hidden="true">&times;</button>'
	        	+'<h4 class="modal-title">新增</h4></div><div class="modal-body">'
	        	+'<p>One fine body&hellip;</p></div><div class="modal-footer">'
	        	+'<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'
	        	+'<button type="button" class="btn btn-success" id="save">保存</button></div></div>'
	        	+'</div></div>');
	        $.get('<%=path%>/esIndex-add.jsp').done(function(html){
	            dialog.modal({
	                keyboard:false
	            }).on({
	                'hidden.bs.modal': function(){
	                    $(this).remove();
	                }
	            }).find('.modal-body').html(html);
	            self.initPage(dialog.find('form'),"");
	        });
	        dialog.find('#save').on('click',{grid: grid}, function(e){
	        	if(!Validator.Validate(dialog.find('form')[0],3))return;
	            	  $.post('${pageContext.request.contextPath}/EsIndex/isSalesIdExist.dhc',
	            			 {'esIndex':dialog.find('#esIndexID').val(),
	            		  	  'salesName':dialog.find('#salesNameID').val()
	            			 }).done(function(result){
	            		  if(result) {
			        		  drgUI.error(esIndex+'业务名或索引已存在，请重新输入!');
			        	  }else {
	                			 $.post('${pageContext.request.contextPath}/EsIndex/add.dhc',dialog.find('form').serialize()).done(function(result){
	                				
	                				 if(result.success ){
	      	  	                        dialog.modal('hide');
	      	  	                        e.data.grid.data('koala.grid').refresh();
	      	  	                        drgUI.success('保存成功！');
	      	  	                    }else{
	      	  	                       drgUI.error('保存失败,'+result.errorMessage);
	      	  	                     }
	      	  	              	});
	                		}
	            	  });
	        });
	    },
	    modify: function(item,id, grid){
	        var self = this;
	        var dialog = $('<div class="modal fade"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title">修改</h4></div><div class="modal-body"><p>One fine body&hellip;</p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button><button type="button" class="btn btn-success" id="save">保存</button></div></div></div></div>');
	        $.get('<%=path%>/esIndex-update.jsp').done(function(html){
	               dialog.find('.modal-body').html(html);
	               self.initPage(dialog.find('form'),item);
	               
	               dialog.find("#salesIdIDUpdate").val(item.salesId);
	               dialog.find("#esIndexIDUpdate").val(item.esIndex);
	               dialog.find("#salesNameIDUpdate").val(item.salesName);
	               dialog.find("#esServeripIDUpdate").val(item.esServerip);
	               dialog.find("#esPortIDUpdate").val(item.esPort);
	               dialog.find("#esFildesIDUpdate").val(item.esFildes);
	               dialog.find("#remarkIDUpdate").val(item.remark);
	               dialog.find("#esCreatePortIDUpdate").val(item.esCreatePort);
	                dialog.modal({
	                    keyboard:false
	                }).on({
	                    'hidden.bs.modal': function(){
	                        $(this).remove();
	                    }
	                });
	                dialog.find('#save').on('click',{grid: grid}, function(e){
	                    if(!Validator.Validate(dialog.find('form')[0],3))return;
          				 $.post('${pageContext.request.contextPath}/EsIndex/update.dhc',
	           					 dialog.find('form').serialize()).done(function(result){
		                        if(result.success){
		                            dialog.modal('hide');
		                            e.data.grid.data('koala.grid').refresh();
		                            drgUI.success('保存成功！');
		                        }else{
		                            drgUI.error(result.actionError);
		                        }
		                    });
	                });
	        });
	    },
	    initPage: function(form,data){
	    	   form.find('.form_datetime').datetimepicker({
                   language: 'zh-CN',
                   format: "yyyy-mm-dd",
                   autoclose: true,
                   todayBtn: true,
                   minView: 2,
                   pickerPosition: 'bottom-left'
               }).datetimepicker('setDate', new Date());//加载日期选择器
               form.find('.select').each(function(){
                    var select = $(this);
                    var idAttr = select.attr('id');
                    select.select({
                        title: '请选择',
                        contents: selectItems[idAttr]
                    }).on('change', function(){
                        form.find('#'+ idAttr + '_').val($(this).getValue());
                    });
               });
	    },
	    
	    remove: function(ids, grid){
	    	$.post('${pageContext.request.contextPath}/EsIndex/delete.dhc', {
	    		salesIds:ids.join(',')
	    	}).done(function(result){
	                        if(result.success){
	                            grid.data('koala.grid').refresh();
	                            drgUI.success('删除成功');
	                        }else{
	                            drgUI.error('error');
	                        }
	    	});
	    }
	}

	PageLoader.initSearchPanel();
	PageLoader.initGridPanel();
	
});
/**
 * 创建ES索引
 */
function createEsIndex(esIndex){
    drgUI.confirm({
        content: '确定要建立索引'+esIndex+'吗,重名索引将会被覆盖？',
        callSure: function(){
            $.post(contextPath+"/EsRestTemplate/deleteEsIndex.dhc",{"esIndex":esIndex}).done(function(result){
                $.post(contextPath+"/EsRestTemplate/createEsIndex.dhc",{"esIndex":esIndex}).done(function(result){
                	  if(result.success) {
                		  drgUI.success(esIndex+'创建成功!');
                	  } else {
                		  debugger
                		  if('Connection refused: connect'==result.errorMessage){
                	    		drgUI.warning('ES服务未启动！'); 
                		  }else{
                	    		drgUI.error(result.errorMessage); 
                		  }
                	  }
                  });
              });
        }
    });    
}
</script>
</head>
<body>
<div style="width:98%;margin-right:auto; margin-left:auto; padding-top: 15px;">
<!-- search form -->

<!-- grid -->
<div data-role="esIndexGrid" id=<%=gridId%>></div>
</div>
</body>
</html>